package com.inter.insideinterservice.newservice;


import com.inter.entity.common.DeviceFlowUsage;
import com.inter.entity.newpojo.Constant;
import com.inter.entity.newpojo.TurntoInterfaceUtils;
import com.inter.entity.newpojo.request.RequestBody.SuspendDataFlowOrdersRequestBody;
import com.inter.entity.newpojo.request.RequestHeader;
import com.inter.entity.newpojo.request.subRequest.SuspendDataFlowOrdersRequest;
import com.inter.entity.newpojo.response.subResponse.SuspendDataFlowOrdersResponse;
import com.inter.utils.HttpClientTool;
import com.inter.utils.JaxbUtil;
import org.apache.log4j.Logger;

public class TurnToSuspendDataFlowOrdersService {

    private static Logger logger = Logger.getLogger(TurnToSuspendDataFlowOrdersService.class);

    public DeviceFlowUsage suspendDataFlowOrders(String deviceId, String orderId, String partnerCode, String secret) {
        try {

            SuspendDataFlowOrdersRequestBody body = new SuspendDataFlowOrdersRequestBody();
            body.setDeviceId(deviceId);
            body.setOrderId(orderId);

            // 请求参数拼接
            SuspendDataFlowOrdersRequest request = new SuspendDataFlowOrdersRequest();
            TurntoInterfaceUtils utils = new TurntoInterfaceUtils();
            //拼装头信息
            RequestHeader header = utils.getRequestHeader(Constant.cmd_singleDeviceMonthFlow, partnerCode);
            request.setHeader(header);
            request.setBody(body);
            String sign = utils.getSign(header, body, body.getClass(), secret);
            //logger.info("签名是：" + sign);
            request.setSign(sign);

            // 将参数转换成XML格式
            String sRequest = new JaxbUtil(SuspendDataFlowOrdersRequest.class).toXml(request, "utf-8");
            //logger.info("请求内容：" + sRequest);
            // 发送post请求，并接收返回结果
            String result = HttpClientTool.sendHttpPost(Constant.BASE_URL, sRequest);
            //logger.info("返回结果：" + result);

            SuspendDataFlowOrdersResponse responseObj = new JaxbUtil(SuspendDataFlowOrdersResponse.class).fromXml(result, "utf-8");
            String retCode = responseObj.getHeader().getResultCode();
            String resultInfo = responseObj.getHeader().getResultInfo();

            if (true == Constant.RET_CODE_0000.equalsIgnoreCase(retCode)) {
                DeviceFlowUsage usage = new DeviceFlowUsage();
                usage.setRetCode(retCode);
                usage.setResultInfo(resultInfo);
                return usage;

            } else {
                DeviceFlowUsage usage = new DeviceFlowUsage();
                usage.setRetCode(retCode);
                usage.setResultInfo(resultInfo);
                return usage;
            }


        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
